﻿using Cxuu.Common;
using Cxuu.Models;
using Cxuu.Service;
using Cxuu.Web.Authorize;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace Cxuu.Web.Admin;

[Area("Admin")]
[Authorize]
public class ConfigController : Base
{
    private readonly ConfigService _configService;
    public ConfigController(ConfigService configService)
    {
        _configService = configService;
    }

    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.Config_Index)]
    public async Task<IActionResult> Index()
    {
        return View(await _configService.GetByNameAsync());
    }


    [HttpPost]
    [XssFieldFilter]//防Xss
    [ActionPermissionFilter(Permission = PermissionKeys.Config_Edit)]
    [Log(Name = "编辑系统配置")]
    public async Task<IActionResult> EditAsync(SytemConfig data)
    {
        if (ModelState.IsValid)
        {
            int res = await _configService.UpdateByNameAsync(data);
            return res > 0 ? Json(new { status = 1, msg = "修改系统配置 " + res + " 项" }) : Json(new { status = 0, msg = "修改系统配置 " });
        }
        else
        {
            var error = ModelState.GetErrorMessages();
            return Json(new { status = 0, msg = "值不能为空：" + error });
        }

    }
}
